<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in">
      <div class="cornerComment-in-title">
        <span>Add Board Data</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <el-form ref="form" :model="form" label-width="70px">
          <el-row :gutter="10">
            <el-col :xs="24" :sm="24" :md="21" :lg="21">
              <el-form-item label="Name" prop="name">
                <el-input v-model="form.name" placeholder="Name"/>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="24" :md="21" :lg="21">
              <el-form-item label="Belonging department" prop="deptId">
                <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="Belonging department"/>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="24" :md="21" :lg="21">
              <el-form-item label="Describe">
                <el-input
                  type="textarea"
                  placeholder="Describe"
                  v-model="form.description"
                  maxlength="100"
                  show-word-limit
                  :autosize="{ minRows: 6, maxRows: 6 }"
                >
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="cornerComment-in-footer">
        <el-button size="small" @click="closePOpup">cancel</el-button>
        <el-button size="small" type="primary" @click="saveBoardFn">Confirm and Save</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {panelAdd, panelDetail, panelEdit} from '@/api/system/board'
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {deptTreeSelect} from "@/api/system/user";

export default {
  props: ['visible', 'detailId'],
  name: "boxDetail",
  components: {Treeselect},
  data() {
    return {
      dialogVisible: false,
      form: {
        name: '',
        description: '',
        deptId: ''
      },
      // 部门树选项
      deptOptions: undefined,
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (this.visible && this.detailId) {
        this.getDetail()
        this.getDeptTree()
      }
      if (!this.visible) {
        this.form = {
          name: '',
          description: ''
        }
      }
    }
  },
  created() {
  },
  methods: {
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    saveBoardFn() {
      if (!this.form.name) {
        this.$message.error('Fill in the name')
        return
      }
      if (this.detailId) {
        this.form.id = this.detailId
        panelEdit(this.form).then(res => {
          if (res.code === 200) {
            this.$message.success(res.msg)
            this.$emit('getList')
            this.closePOpup()
          } else {
            this.$message.error(res.msg)
          }
        })
      } else {
        panelAdd(this.form).then(res => {
          if (res.code === 200) {
            this.$message.success(res.msg)
            this.$emit('getList')
            this.closePOpup()
          } else {
            this.$message.error(res.msg)
          }
        })
      }
    },
    getDetail() {
      panelDetail(this.detailId).then(res => {
        console.log('res:', res)
        let {data} = res
        this.form.name = data.name
        this.form.deptId = data.deptId
        this.form.description = data.description
      })
    },
    closePOpup() {
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 600px;
  height: 400px;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
  overflow: hidden;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
  background: #fff;

}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
  background: #fff;
}

.commentBody {
  padding: 20px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

</style>
